﻿#include "FITKAcousticsHDF5Reader.h"
#include "FITKAcousticsHDF5IOInterface.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsFluid.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsIsosolid.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsViscoElastic.h"
#include <H5Cpp.h>
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITKAcousticsPhysicsAdaptor.h"
#include "FITKAcousticsDiscModelAdaptor.h"
namespace IO
{
    using namespace Acoustics;
    FITKAcousticsHDF5Reader::FITKAcousticsHDF5Reader(FITKAcousticsHDF5IOInterface* interface, bool & success)
        :_compInterface(interface), _isSuccess(success)
    {
        if (_compInterface == nullptr) return;
        //获取数据
        _discModel = _compInterface->getDataObjectT<Acoustics::FITKAcousticsDiscModel>("Model");
        _physics = _compInterface->getDataObjectT<Acoustics::FITKAcousticsPhysics>("Physics");

        //清空数据，准备填充新数据
        _discModel->clearDiscModel();
        _physics->clearAcousticsPhysics();
    }

    FITKAcousticsHDF5Reader::~FITKAcousticsHDF5Reader()
    {

    }
    void FITKAcousticsHDF5Reader::consoleMessage(int level, const QString& str)
    {
        switch (level)
        {
        case 1:AppFrame::FITKMessageNormal(str); break;
        case 2: AppFrame::FITKMessageWarning(str); break;
        case 3:AppFrame::FITKMessageError(str); break;
        default: AppFrame::FITKMessageError(str); break;
        }
    }

    void FITKAcousticsHDF5Reader::run()
    {
        QString err;
        bool Result = this->createContext(err);//创建hdf5文件指针
        if (Result == false)
        {
            consoleMessage(3, "create hdf5 Context err");
            return;
        }
        if (_physics)
        {
            FITKAcousticsPhysicsAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsPhysicsAdaptor>("FITKAcousticsHDF5", _physics);
            if (adaptor == nullptr) return;
            adaptor->setReader(this);
            adaptor->setDataObject(_physics);
            Result &= adaptor->adaptR();
            delete adaptor;
        }


        if (_discModel != nullptr)
        {
            //调用适配器
            FITKAcousticsDiscModelAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsDiscModelAdaptor>("FITKAcousticsHDF5", _discModel);
            if (adaptor == nullptr) return;
            adaptor->setReader(this);
            adaptor->setDataObject(_discModel);
            Result &= adaptor->adaptR();
            delete adaptor;
        }
    }

}
